package io.github.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.github.dao.ExtensionDao;
import io.github.entity.AnswerGroupEntity;
import io.github.entity.ExtensionEntity;
import io.github.service.AnswerGroupService;
import io.github.service.ExtensionService;
import io.github.util.CommandUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Max
 * <br>
 * date 2019/12/27 16:05
 */

@Service
public class ExtensionServiceImpl extends ServiceImpl<ExtensionDao, ExtensionEntity> implements ExtensionService {

    @Resource
    ExtensionDao extensionDao;

    @Autowired
    AnswerGroupService answerGroupService;


    @Override
    public Page<ExtensionEntity> queryListPage(Integer offset, Integer limit, String number,Boolean status,Integer gid) {
        Page page = new Page<>(offset,limit);

        List<ExtensionEntity> list = extensionDao.list(page, number,gid);

        page.setRecords(list);
        return page;
    }

    @Override
    public void save(ExtensionEntity extensionEntity) throws IOException {
        //判断是否已经存在分机
        String number = extensionEntity.getNumber();
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("number",number);
        List list = baseMapper.selectList(wrapper);
        if(list.size()>0) {
            throw new IllegalArgumentException("分机号已经存在");
        }
        extensionEntity.setCreateTime(new Date());
        baseMapper.insert(extensionEntity);
        AnswerGroupEntity group = answerGroupService.getById(extensionEntity.getAnswerGroup());
        CommandUtils.exeAdd(extensionEntity.getNumber(),extensionEntity.getPassword(),group.getName());
        CommandUtils.reload();
    }

    @Override
    public void deleteBatch(Long[] Ids) {
        for(int i=0;i<Ids.length;i++) {
            ExtensionEntity entity = baseMapper.selectById(Ids[i]);
            String number = entity.getNumber();
            //删除配置文件
            File file = new File("/usr/local/nfp/conf/directory/default/"+number+".xml");
            file.delete();
        }
        CommandUtils.reload();
        baseMapper.deleteBatchIds(Arrays.asList(Ids));
    }

    @Override
    public void updateExtension(ExtensionEntity entity) throws IOException {

        Wrapper<ExtensionEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("id",entity.getId());

        //获取修改之前的数据
        ExtensionEntity entityBefore = baseMapper.selectById(entity.getId());

        AnswerGroupEntity answerGroup = answerGroupService.getById(entity.getAnswerGroup());

        if(!entityBefore.getAnswerGroup().equals(entity.getAnswerGroup())) {
            File file = new File("/usr/local/nfp/conf/directory/default/"+entity.getNumber()+".xml");
            file.delete();
            CommandUtils.exeAdd(entity.getNumber(),entity.getPassword(),answerGroup.getName());
            CommandUtils.reload();
        }

        try {
            if(!entityBefore.getClockTime().equals(entity.getClockTime())){
                entity.setCount(2);
            }
        } catch (NullPointerException e) {
            entity.setCount(2);
        }
        baseMapper.update(entity,wrapper);
    }


    @Override
    public void updateClock(JSONObject json) {
        JSONArray ids = json.getJSONArray("ids");
        String date = json.getString("date");

        Object[] id = ids.toArray();

        extensionDao.updateBatch(id,date);
    }

    @Override
    public void simSeqEdit(JSONObject json) {
        Integer id = json.getInteger("id");
        String groupNo = json.getString("groupNo");
        extensionDao.simSeqEdit(id,groupNo);
    }

}